package com.ebt.m.utils;

import android.os.CountDownTimer;
import android.util.Log;

import java.util.HashMap;

/**
 * Created by Rick.Wang on 2017/1/10.
 */
public class CountDownManager {

    private static final long TIME_SPAN = 60 * 1000;
    private static CountDownManager instance;

    private boolean mIsCountingDown = false;
    private CountDown mCountDown;
    private CountDownListener mListener;
    private CountDownErrorMessageListener mMessageListener;

    //暂时不用
    private HashMap<String, Boolean> mHasSend = new HashMap<>();

    private CountDownManager(){}

    public static CountDownManager getInstance(){
        if (instance == null) {
            synchronized (CountDownManager.class) {
                if (instance == null) {
                    instance = new CountDownManager();
                }
            }
        }
        return instance;
    }

    public void setCountDownErrorMessageListener(CountDownErrorMessageListener listener){
        mMessageListener = listener;
    }

    public CountDownErrorMessageListener getCountDownErrorMessageListener(){
        return mMessageListener;
    }

    public void setCountDownListener(CountDownListener listener){
        mListener = listener;
    }

    public CountDownListener getCountDownListener(){
        return mListener;
    }

    public boolean isCountingDown(){
        return mIsCountingDown;
    }

    class CountDown extends CountDownTimer {

        public CountDown(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            mIsCountingDown = true;
            if (mListener != null) {
                mListener.onTick(millisUntilFinished);
            }
        }

        @Override
        public void onFinish() {
            mIsCountingDown = false;
            if (mListener != null) {
                mListener.onFinish();
            }
        }
    }

    //计时器实时回调
    public interface CountDownListener {
        void onTick(long remainMillisSecond);
        void onFinish();
    }

    //计时器提示
    public interface CountDownErrorMessageListener {
        void onCounting();
    }

    //验证码开始计时
    public void startCountDown(){
        if (isCountingDown()){
            if (mMessageListener != null){
                mMessageListener.onCounting();
            }
            return;
        }
        if (mCountDown == null) {
            mCountDown = new CountDown(TIME_SPAN, 1000);
        }
        mCountDown.start();
    }

    public void restartCountDown(){
        if (mCountDown != null) {
            mCountDown.cancel();
        }else{
            mCountDown = new CountDown(TIME_SPAN, 1000);
        }
        mCountDown = new CountDown(TIME_SPAN, 1000);
        mCountDown.start();
    }

    public void releaseRef(){
        mListener = null;
    }

    public void setHasSend(String className, Boolean hasSend){
        mHasSend.put(className, hasSend);
    }

    public boolean getHasSend(String className){
        Boolean ret = mHasSend.get(className);
        if (ret != null && ret){
            return true;
        }
        return false;
    }
}